package edu.uestc.fly.performancetest.gpu;

import android.content.Context;
import android.os.AsyncTask;

import edu.uestc.fly.performancetest.constant.Constant;
import edu.uestc.fly.performancetest.io.DBManager;
import edu.uestc.fly.performancetest.utils.ProgressListen;

/**
 * Created by flyln on 2017/10/23 15:42.
 * email:xyf_uestc@163.com
 *
 * String*********对应我们的URL类型
 * Integer********进度条的进度值
 * BitMap*********异步任务完成后返回的类型
 *
 * 当前文件没有用到
 * 
 */
class GPUAsyncTask extends AsyncTask<String, Integer, Double> {

    public Context context;
    public ProgressListen progresslistener;
    public Constant.TaskType taskType = Constant.TaskType.IO;
    private DBManager dbManager;


    GPUAsyncTask(Context context){
        this.context = context;
        dbManager = new DBManager(context);
    }

    //执行异步任务（doInBackground）之前执行，并且在ui线程中执行
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


        if (progresslistener != null){
            progresslistener.initProgress(taskType,0);
        }


    }

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub



        int size =1000;
        int i,length = size;
        char oldArray[] = new char[size];
        long StartTime = System.currentTimeMillis();

        for (i = 0 ; i < size ; i ++ ){


            publishProgress((int)( (float)i / (float)size * 100 ));
        }

        long EndTime = System.currentTimeMillis();

        System.out.println("消耗时间: " + (EndTime - StartTime)+" ms.");




        return (double)EndTime - StartTime;
    }
    //在ui线程中执行 可以操作ui
    @Override
    protected void onPostExecute(Double runTime) {
        // TODO Auto-generated method stub
        super.onPostExecute(runTime);
        //任务完成，对话框进度条隐藏

        if (progresslistener != null){
            progresslistener.closeProgress(taskType,runTime);
        }
    }
    /*
     * 在doInBackground方法中已经调用publishProgress方法 更新任务的执行进度后
     * 调用这个方法 实现进度条的更新
     * */
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        if (progresslistener != null){
            progresslistener.updateProgress(taskType,values[0]);
        }
    }


}